package example;

import example.entity.User;
import example.service.BookService;
import example.service.BorrowService;
import example.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 张心怡
 * @date 2024/12/16
 */
@WebServlet("/adminDashboardServlet")
public class AdminDashboardServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取用户总数
        int totalUsers = UserService.getTotalUsers();
        // 获取图书总数
        int totalBooks = BookService.getTotalBooks();
        // 获取当前借阅数量
        User user = (User) request.getSession().getAttribute("user");
        int borrowedBooksCount = 0;
        if (user.getType() == 0) {
            borrowedBooksCount = BorrowService.getBorrowedBooksCount(user.getId());
        } else {
            borrowedBooksCount = BorrowService.getBorrowedBooksCount(null);
        }

        // 将数据设置到请求属性中
        request.setAttribute("totalUsers", totalUsers);
        request.setAttribute("totalBooks", totalBooks);
        request.setAttribute("borrowedBooksCount", borrowedBooksCount);

        // 转发到管理员控制面板页面
        request.getRequestDispatcher("/admin_dashboard.jsp").forward(request, response);
    }
}